﻿using DBHelper;
using DocumentFormat.OpenXml.Packaging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using S = DocumentFormat.OpenXml.Spreadsheet.Sheets;//必须
using E = DocumentFormat.OpenXml.OpenXmlElement;//必须
using A = DocumentFormat.OpenXml.OpenXmlAttribute;//必须
using System.IO;
using System.Reflection;
using System.Threading;

namespace Common
{
    public static class Tools
    {
        #region 获得两个日期的间隔分钟(除去指定的时间范围)
        /// <summary>
        /// 获得两个日期的间隔小时数(除去指定的时间范围),直接调用 DifferDate
        /// </summary>
        /// <param name="StartDateTime">开始时间</param>
        /// <param name="EndDateTime">结束时间</param>
        /// <param name="holidaysList">节假日表</param>
        /// <param name="x">x=1除去指定时间的范围，x=2不除去指定时间范围</param>
        /// <returns></returns>
        public static int DifferDate(DateTime StartDateTime, DateTime EndDateTime, List<string> holidaysList, int x)
        {
            if (EndDateTime < StartDateTime)
            {
                return 0;
            }
            string sdate = StartDateTime.ToString("yyyy-MM-dd");
            string edate = EndDateTime.ToString("yyyy-MM-dd");
            TimeSpan sp = EndDateTime.Subtract(StartDateTime);
            int intDiffer = sp.Days;//相差天数的int值
            int Workday = 0;//中间工作日的天数
            int Secondsum = 0;//总秒数
            string str;

            for (int i = 1; i <= intDiffer; i++)
            {
                str = StartDateTime.AddDays(i).ToString("yyyy-MM-dd");
                if (str != edate)
                {
                    if (!holidaysList.Contains(str))
                    {
                        Workday++;
                    }
                }

            }

            if (x == 1)//计算除去指定时间内和节假日的秒数
            {
                Secondsum = Workday * 39600;//39600=11小时*3600秒
                if (sdate == edate && !holidaysList.Contains(sdate))//如果是同一天并且不是节假日
                {
                    Secondsum += fun1(StartDateTime, EndDateTime);
                }
                else if (sdate != edate)//如果不是同一天
                {
                    //计算开始这天的秒数
                    if (!holidaysList.Contains(sdate))
                    {
                        Secondsum += fun2(StartDateTime);
                    }
                    //计算结束这天的秒数
                    if (!holidaysList.Contains(edate))
                    {
                        Secondsum += fun3(EndDateTime);
                    }
                }

            }
            else if (x == 2)//计算没有除去指定时间但是除去了节假日的秒数
            {
                TimeSpan timed1 = new TimeSpan(StartDateTime.Hour, StartDateTime.Minute, StartDateTime.Second);
                TimeSpan timed2 = new TimeSpan(EndDateTime.Hour, EndDateTime.Minute, EndDateTime.Second);
                TimeSpan time00 = new TimeSpan(00, 00, 00);
                TimeSpan time2359 = new TimeSpan(23, 59, 59);
                TimeSpan jg;
                Secondsum = Workday * 86400;//86400=24小时*3600秒
                if (sdate == edate && !holidaysList.Contains(sdate))//如果是同一天并且不是节假日
                {
                    TimeSpan ts = EndDateTime - StartDateTime;
                    Secondsum += ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
                }
                else if (sdate != edate)//如果不是同一天
                {
                    //计算开始这天的秒数
                    if (!holidaysList.Contains(sdate))
                    {
                        jg = time2359 - timed1;
                        Secondsum += jg.Hours * 3600 + jg.Minutes * 60 + jg.Seconds;
                    }
                    //计算结束这天的秒数
                    if (!holidaysList.Contains(edate))
                    {
                        jg = timed2 - time00;
                        Secondsum += jg.Hours * 3600 + jg.Minutes * 60 + jg.Seconds;
                    }
                }
            }
            return Secondsum;
        }
        public static int fun1(DateTime d1, DateTime d2)//开始和结束是同一天
        {
            TimeSpan timed1 = new TimeSpan(d1.Hour, d1.Minute, d1.Second);
            TimeSpan timed2 = new TimeSpan(d2.Hour, d2.Minute, d2.Second);
            TimeSpan time830 = new TimeSpan(08, 30, 00);
            TimeSpan time21 = new TimeSpan(21, 00, 00);
            TimeSpan ts;
            int jg = 0;
            if (timed1 <= time830) //一,如果d1<=8:30
            {
                if (timed2 <= time21)
                {
                    ts = timed2 - time830;
                    jg = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
                }
                else if (timed2 >= time21)
                {
                    jg = 45000;
                }
            }
            else if (timed1 >= time830 && timed1 <= time21)//二,如果d1>=8:30 and d1<=12:00
            {
                if (timed2 <= time21)
                {
                    ts = timed2 - timed1;
                    jg = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
                }

                else if (timed2 >= time21)
                {
                    ts = time21 - timed1;
                    jg = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
                }
            }
            else if (timed1 >= time21)//七,如果d1>=21:00
            {
                jg = 0;
            }
            return jg;
        }
        public static int fun2(DateTime d1)//开始和结束不是同一天,计算开始这天的用时
        {
            TimeSpan timed1 = new TimeSpan(d1.Hour, d1.Minute, d1.Second);
            TimeSpan time830 = new TimeSpan(08, 30, 00);
            TimeSpan time21 = new TimeSpan(21, 00, 00);
            TimeSpan ts;
            int jg = 0;
            if (timed1 <= time830)
            {
                jg = 45000;
            }
            else if (timed1 <= time21)
            {
                ts = time21 - timed1;
                jg = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
            }
            else if (timed1 >= time21)
            {
                jg = 0;
            }
            return jg;
        }
        public static int fun3(DateTime d2)//开始和结束不是同一天,计算结束这天的用时
        {
            TimeSpan timed2 = new TimeSpan(d2.Hour, d2.Minute, d2.Second);
            TimeSpan time830 = new TimeSpan(08, 30, 00);
            TimeSpan time21 = new TimeSpan(21, 00, 00);
            TimeSpan ts;
            int jg = 0;
            if (timed2 <= time830)
            {
                jg = 0;
            }
            else if (timed2 <= time21)
            {
                ts = timed2 - time830;
                jg = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
            }
            else if (timed2 >= time21)
            {
                jg = 45000;
            }
            return jg;
        }

        #endregion
    }
}
